package com.loe.ui.quickjs

import android.view.View
import android.view.animation.*
import android.webkit.JavascriptInterface
import com.loe.mvp.util.Animator
import com.loe.ui.jsContext
import com.quickjs.JSFunction
import com.quickjs.JSObject

class UiAnimator(private val view: View) : JSObject(jsContext)
{
    private val animator = Animator(view)

    init
    {
        appendJavascriptInterface(object
        {
            @JavascriptInterface
            fun setTime(t: Int)
            {
                animator.time = t.toLong()
            }

            @JavascriptInterface
            fun setPivot(pivotX: Double, pivotY: Double)
            {
                animator.setPivot(pivotX, pivotY)
            }

            @JavascriptInterface
            fun setVisible(visible: Boolean)
            {
                animator.setVisible(visible)
            }

            @JavascriptInterface
            fun setInterpolator(s: String, factor: Double)
            {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1)
                {
                    val factor = factor.toFloat()
                    animator.setInterpolator(
                        when (s)
                        {
                            "accelerate" -> AccelerateInterpolator(factor)
                            "accelerateDecelerate" -> AccelerateDecelerateInterpolator()
                            "decelerate" -> DecelerateInterpolator(factor)
                            "bounce" -> BounceInterpolator()
                            "anticipate" -> AnticipateInterpolator(factor)
                            "anticipateOvershoot" -> AnticipateOvershootInterpolator(factor)
                            "overshoot" -> OvershootInterpolator(factor)
                            "cycle" -> CycleInterpolator(factor)
                            // Linear
                            else -> LinearInterpolator()
                        }
                    )
                }
            }

            @JavascriptInterface
            fun setEndListener(callback: JSFunction)
            {
                animator.setOnAnimateEndListener { callback.call(this@UiAnimator, null) }
            }

            @JavascriptInterface
            fun alpha(fromAlpha: Double, toAlpha: Double)
            {
                animator.alpha(fromAlpha, toAlpha)
            }

            @JavascriptInterface
            fun scale(fromX: Double, toX: Double, fromY: Double, toY: Double)
            {
                animator.scale(fromX, toX, fromY, toY)
            }

            @JavascriptInterface
            fun scaleAlpha(fromX: Double, toX: Double, fromY: Double, toY: Double, fromAlpha: Double, toAlpha: Double)
            {
                animator.scaleAlpha(fromX, toX, fromY, toY, fromAlpha, toAlpha)
            }

            @JavascriptInterface
            fun trans(fromX: Double, toX: Double, fromY: Double, toY: Double)
            {
                animator.trans(fromX, toX, fromY, toY)
            }

            @JavascriptInterface
            fun transAbs(fromX: Double, toX: Double, fromY: Double, toY: Double)
            {
                animator.transAbs(fromX, toX, fromY, toY)
            }

            @JavascriptInterface
            fun transParent(fromX: Double, toX: Double, fromY: Double, toY: Double)
            {
                animator.transParent(fromX, toX, fromY, toY)
            }

            @JavascriptInterface
            fun transAlpha(fromX: Double, toX: Double, fromY: Double, toY: Double, fromAlpha: Double, toAlpha: Double)
            {
                animator.transAlpha(fromX, toX, fromY, toY, fromAlpha, toAlpha)
            }
        })
    }
}